<?php
/**
*btc列表
*/
require_once 'common.inc.php';
include_once 'include/mysql_info.php';


$tracker = new dbstuff;
$tracker->connect($dsdbhost, $dsdbuser, $dsdbpw, $traname, $dspconnect);
$tra_db = $tracker;
//TrackerTactic数据库专用链接
error_reporting(E_ALL);
$matches=parse_url($_SERVER['HTTP_REFERER']);
//$url=$matches['scheme'].'://'.$matches['host'].'/';
if('editsendport'==$action){
	include 'include/sendport_info.php';
	for($y=0;$y<$rows;$y++){

		if($choose_ip==1||$sendport[$y]!=$port[$y])$sendport[$y]=$port[$y];

		$ip_info.="\$acsip[$y]='".$acsip[$y]."';\n\$sendport[$y]='".$sendport[$y]."';\n";
	}
	$filename="include/sendport_info.php";
	$fp=fopen($filename,'wb+');
		if($fp)
		{
			$str="<?php\n".$ip_info."?>";		
			fwrite($fp,$str);			
			fclose($fp);
		}	
	adminmsg('修改成功','acsshow.inc.php?action=list');

}
elseif('list'==$action)
{	
/*	$limit = 10;
	$page = intval($page) < 1?1:intval($page);
	$start = (intval($page)-1) * $limit;
	$sql = "select count(*) from acsconnect ";
	$query = $tra_db -> query($sql);
	$count = $tra_db -> fetch_array($query);
	$allpage = ceil($count['count(*)'] / $limit);
	$page = $page > $allpage?$allpage:$page;
	$url = 'acsshow.inc.php?action=' . $action . '&amp;';
	$pages = numofpage($count['count(*)'], $page, $allpage, $url);
	
	$i=0;
	$sql="select * from btc order by ip desc";
	$query=$tra_db->query($sql);
	while($result=$tra_db->fetch_array($query)){
		$row[]=$result;
		$ip_info.="\$acsip[$i]='".$result['ip']."';\n\$sendport[$i]='".$port."';\n";
		$i++;
	}

	$filename="include/sendport_info.php";
	$fp=fopen($filename,'wb+');
		if($fp)
		{
			$str="<?php\n".$ip_info."?>";		
			fwrite($fp,$str);			
			fclose($fp);
		}
*/
	//显示当前ACS的IP和端口
	$listip=array();
	$oldip=null;

	$sql="select * from acsconnect order by aip asc";
	$query=$tra_db->query($sql);

	while($result=$tra_db->fetch_array($query)){
			//$acsconnectip[]=$result;	
			if(!in_array($result['aip'],$listip))
			{
				$oldip=$result['aip'];
				$listip[]=$oldip;				
			}
	}
	//getConnectNum();

	include_once MCC_ROOT.'/templates/msystem/acsshowlist.htm';
}
elseif('search'==$action)
{	
	$stimeo=$stime;
	$etimeo=$etime;
	if(!ereg("-",$stimeo))$stimeo=date('Y-m-d',$stimeo);
	if(!ereg("-",$etimeo))$etimeo=date('Y-m-d',$etimeo);
	if(empty($page))
	{
		$stime=GmtToUnix($stime);
		$etime=GmtToUnix($etime);
	}
	if($stime>$etime)
		adminmsg('时间参数错误','acsshow.inc.php?action=list');	
	else
	{
		$limit = 10;
		$page = intval($page) < 1?1:intval($page);
		$start = (intval($page)-1) * $limit;
		$sql = "select count(*) from acsconnect where atime>='$stime' and atime<='$etime'";
		$query = $tra_db -> query($sql);
		$count = $tra_db -> fetch_array($query);
		$allpage = ceil($count['count(*)'] / $limit);
		$page = $page > $allpage?$allpage:$page;
		$url = 'acsshow.inc.php?action=' . $action . '&amp;stime='.$stime.'&amp;etime='.$etime.'&amp;';
		$pages = numofpage($count['count(*)'], $page, $allpage, $url);

		$sql="select * from acsconnect where atime>='$stime' and atime<='$etime' order by acount desc limit $start,$limit";
		$query=$tra_db->query($sql);
		while($result=$tra_db->fetch_array($query)){
			$acsconnectip[]=$result;
		}
	$sql="select * from btc order by ip desc";
	$query=$tra_db->query($sql);
	while($result=$tra_db->fetch_array($query)){
		$row[]=$result;
	}
	include_once 'include/sendport_info.php';
	include_once MCC_ROOT.'/templates/msystem/acsshowlist.htm';
	}
}
elseif('edit'==$action)
{
	$action = 'oedit';
	if ($ip) {
		$oip=$ip;
		$list = array();
		$sql = "select * from btc where ip='$ip'";
		$query = $tra_db -> query($sql);
		$list = $tra_db -> fetch_array($query);
	} 
	include_once MCC_ROOT . '/templates/msystem/acsa.htm';

}
elseif ('oedit' == $action) 
{
	if ($ip) {
		$tra_db -> query("update btc set ip='$ip' where ip='$oip'");
	} 
	adminmsg('修改成功', 'acsshow.inc.php?action=list');
}
elseif('delet'==$action)
{
	$sql="delete from btc where ip='$ip'";
	$tra_db-> query($sql);
	adminmsg('删除成功', 'acsshow.inc.php?action=list');
}
elseif('delet1'==$action)
{
	$sql = "delete from acsconnect where aip='$aip' and agetport='$agetport' and asendport='$asendport'";
	$tra_db-> query($sql);
	adminmsg('删除成功', 'acsshow.inc.php?action=list');
}
elseif('add'==$action)
{
	$test=$_POST['ip'];
	echo $test;
	$action = 'oadd';
	include_once MCC_ROOT . '/templates/msystem/acsa.htm';
}
elseif('oadd'==$action)
{
	if (!$ip)
		adminmsg('没有填写ip及端口', 'acsshow.inc.php?action=add');
	$sql = "select * from btc where ip='$ip'";
	$query = $tra_db -> query($sql);
	$result = $tra_db -> fetch_array($query);
	if ($result) {
		adminmsg('ip与端口已存在', 'acsshow.inc.php?action=add');
	} else {
		$sql = "insert into btc(tacticid,ip) values ('10','$ip')";
		$tra_db -> query($sql);
		adminmsg('添加成功', 'acsshow.inc.php?action=list');
	}
}



function getConnectNum()
{	
	global $tra_db;
	$tracker=array();
	$query=$tra_db->query("select * from acsconnect order by aip,atime desc limit 10");
   	$kl=0;
	$qu="";
	while($tracker=$tra_db->fetch_array($query))
	{
		$str="";
		$str=explode(':',$tracker['aip']);
		$acshost[]=$str[0];
		$acsport[]=$str[1];
		$port=$acsport[$kl];
		$newtime=explode(" ",microtime());
		$atime=$newtime[1];
		echo "<br>发送协议：{$acshost[$kl]}主机，端口号：{$acsport[$kl]}信息：";
		$str="21321";
		//if($str=sendin($msg,$host,$port))
		$tra_db->query(" insert into acsconnect(aip,agetport,asendport,atime,acount) values ('".$acshost[$kl]."','".$acsport[$kl]."','".$port."','".$atime."','".$str."'); ");
		$kl++;
	}
}	

function GmtToUnix($GmtDate)
{
 $DateArr = explode(' ',$GmtDate); // 分割GMT日期为 日期 | 时间

 /* 在日期中取得年,月,日 */
 $pDate = split('[/.-]',$DateArr[0]);
 $Year = $pDate[0];
 $Month = $pDate[1];
 $Day = $pDate[2];
 
 /* 在时间中取得时,分,秒 */
 $pTime = split('[:.-]',$DateArr[1]);
 $Hour = $pTime[0];
 $Minute = $pTime[1];
 $Second = $pTime[2];
 
 if($Year == '' || !is_numeric($Year))$Year = 0;
 if($Month == '' || !is_numeric($Month))$Month = 0;
 if($Day == '' || !is_numeric($Day))$Day = 0;
 if($Hour == '' || !is_numeric($Hour))$Hour = 0;
 if($Minute == '' || !is_numeric($Minute))$Minute = 0;
 if($Second == '' || !is_numeric($Second))$Second = 0;
 return mktime($Hour,$Minute,$Second,$Month,$Day,$Year);
}

?>